#include"BlockQueue.hpp"
#include<unistd.h>
#include"Task.hpp"

struct ThreadData
{
    BlockQueue<Task>* bq;
    std::string name;
};
void* consumer(void* args)
{
    ThreadData* td=static_cast<ThreadData*>(args);
    while(1)
    {
        //读数据
        Task t;
        td->bq->Pop(&t);
        t();
        std::cout<<"执行任务"<<" ";
        t.Print();
    }
}

void* productor(void* args)
{
    ThreadData* td=static_cast<ThreadData*>(args);
    int data=1;
    while(1)
    {
        int x=data;
        int y=data+1;
        Task t(x,y);

        td->bq->Enqueue(t);
        std::cout<<"生产任务"<<std::endl;
        data++;
    }
}
int main()
{
    //单生产单消费模型
    BlockQueue<Task>* bq=new BlockQueue<Task>();
    pthread_t c,p;

    ThreadData ctd={bq,"消费者"};
    pthread_create(&c,nullptr,consumer,(void*)&ctd);

    ThreadData ptd={bq,"生产者"};
    pthread_create(&p,nullptr,productor,(void*)&bq);

    //生产者和消费者能看到同一个队列
    pthread_join(c,nullptr);
    pthread_join(p,nullptr);

    delete bq;
    return 0;
}